﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using OgilvyOne.DALFactory;
using OgilvyOne.Utility;


namespace OgilvyOne.BLL
{
    public class CmsArticleBll
    {
        /// 查询文章
        /// </summary>
        /// <param name="caq"></param>
        /// <returns></returns>
        public IList getCmsArticleList(CmsArticleQuery caq)
        {
            return CmsArticleFactory.create().getCmsArticleList(caq);
        }
        public IList getCmsArticleList(string str)
        {
            return CmsArticleFactory.create().getCmsArticleList(str);
        }
        /// <summary>
        /// 批量删除文章
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DelCmsArticle(string ids)
        {
            if (ids != null && ids != "")
            {               
                if (CmsArticleFactory.create().Del(ids))
                {                   
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 获取单篇文章
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public CmsArticleModel GetCmsArticleModel(int aid)
        {
            return CmsArticleFactory.create().getCmsArticle(aid);
        }
        /// <summary>
        /// 新增文章
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public bool Add(CmsArticleModel article)
        {
            return CmsArticleFactory.create().Insert(article);
        }
        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public bool Update(CmsArticleModel article)
        {
            return CmsArticleFactory.create().Update(article);
        }
        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public bool Pubone(int aid, string pubuname)
        {
            if (CmsArticleFactory.create().UpdateState(aid, "1", pubuname))
            {
                CmsArticleModel cm = CmsArticleFactory.create().getCmsArticle(aid);

                CmsArticleFrontFactory.create().PushFront(cm);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 批量发布
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool Pubs(string ids, string pubuname)
        {
            string[] idss = ids.Split(',');
            if (idss.Length > 0)
            {

                int aid = int.Parse(idss[0]);

                CmsArticleModel model = GetCmsArticleModel(aid);
               
                foreach (string id in idss)
                {
                    if (id != null && id != "" && id != ",")
                    {
                        Pubone(int.Parse(id), pubuname);

                     }
                }
            }
            return true;
        }
        /// <summary>
        /// 批量审核
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool Audits(string ids, string state, string auditname)
        {
            string[] idss = ids.Split(',');
            foreach (string id in idss)
            {
                if (id != null && id != "" && id != ",")
                {
                    Audit(int.Parse(id), state, auditname);
                }
            }

            return true;
        }
        /// <summary>
        /// 审核文章
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public bool Audit(int aid, string state, string auditname)
        {
            return CmsArticleFactory.create().AudutArticle(aid, state, auditname);
        }
        /// <summary>
        /// 撤消发布
        /// </summary>
        /// <param name="aid"></param>
        /// <param name="state"></param>
        /// <param name="pubname"></param>
        /// <returns></returns>
        public bool RePubOne(int aid, string pubname)
        {
            if (CmsArticleFactory.create().UpdateState(aid, "6", pubname))
            {
                CmsArticleFrontFactory.create().Del(aid.ToString());
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改文章SEO
        /// </summary>
        /// <param name="seo"></param>
        /// <returns></returns>
        public bool UpdateSeo(CmsArticleModel seo)
        {
            if (CmsArticleFactory.create().UpdateSeo(seo))
            {
                return CmsArticleFactory.create().UpdateState(seo.Articleid, "0", seo.Modifyusername);
            }
            return false;
        }
        /// <summary>
        /// 获取文章SEO属性
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public CmsArticleModel GetCmsArticelSeo(int aid)
        {
            return CmsArticleFactory.create().GetCmsArticelSeo(aid);
        }

        public bool UpdateAudit(int aid, string username)
        {
            return CmsArticleFactory.create().UpdateState(aid, "0", username);
        }


        /// <summary>
        /// 修改排序号
        /// </summary>
        /// <param name="aid"></param>
        /// <param name="next">0 上 1 下</param>
        /// <returns>修改排序号</returns>
        public int ModOrderNum(int aid, int next)
        {
            CmsArticleModel cm = GetCmsArticleModel(aid);
            int ordernum = cm.OrderNum;
            int temcmid = CmsArticleFactory.create().GetOrderArticleId(cm.Columnid, ordernum, next);
            if (temcmid > 0)
            {
                CmsArticleModel temcm = CmsArticleFactory.create().getCmsArticle(temcmid);
                CmsArticleFactory.create().ModOrderNum(aid, temcm.OrderNum);
                CmsArticleFactory.create().ModOrderNum(temcm.Articleid, ordernum);

                return temcm.OrderNum;
            }
            return 0;

        }

        /// <summary>
        /// 栏目最大排序号
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public int MaxOrder(int cid)
        {
            return CmsArticleFactory.create().MaxOrder(cid);
        }



        

        /// <summary>
        /// 栏目发布文章列表
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public IList GetColumnAllPubArticleList(int cid)
        {
            return CmsArticleFrontFactory.create().GetArticleOrderList(cid, 0);
        }

        /// <summary>
        /// 所有层级栏目发布文章列表
        /// </summary>
        /// <param name="layerid"></param>
        /// <param name="row"></param>
        /// <returns></returns>
        public IList GetLayerPubArticleList(string layerid, int row)
        {
            return CmsArticleFrontFactory.create().GetLayerArticleList(layerid, row);
        }

        public bool AddHits(int aid)
        {
            return CmsArticleFrontFactory.create().addHits(aid);
        }
        /// <summary>
        /// 阅读数
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public int GetHits(int aid)
        {
            return CmsArticleFrontFactory.create().GetArticleHits(aid);
        }
        /// <summary>
        /// 收藏数
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public int GetColls(int aid)
        {
            return CmsArticleFrontFactory.create().GetArticleColls(aid);
        }

        public ICmsArticleFront GetICmsArticleFront()
        {
            return CmsArticleFrontFactory.create();
        }
         
    }
}
